﻿using System;
using System.Collections.Generic;
using System.Resources;
using System.Windows.Forms;
using System.Reflection;

using BotonJXC.Forms.Controls;
using BotonJXC.Util;
using BotonJXC.Forms.Events;

namespace BotonJXC.Forms
{
    public partial class BaseInfoForm : BaseForm
    {
        /// <summary>
        /// 对象选择事件
        /// </summary>
        public event BaseInfoSelectedEventHandler BaseInfoSelected = null;
        /// <summary>
        /// 双击选择一行数据时发生的事件
        /// </summary>
        IDictionary<string, BaseInfoControl> _controls = new Dictionary<string, BaseInfoControl>();
        IDictionary<string, string> _nodeDics = new Dictionary<string, string>();
        BaseInfoControl _curControl = null;
        bool _changeModule = true;
        bool _enabledSelect = false;

        public BaseInfoForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 获取或设置是否可切换模块
        /// </summary>
        public bool ChangeModule
        {
            get
            {
                return _changeModule;
            }
            set
            {
                _changeModule = value;
            }
        }

        public bool EnabledSelect
        {
            get
            {
                return _enabledSelect;
            }
            set
            {
                _enabledSelect = value;
            }
        }
        private void LoadBaseInfoControl(string tag)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();

            if (_curControl != null)
            {
                _curControl.Visible = false;
            }
            BaseInfoControl value = null;
            if (!_controls.TryGetValue(tag, out value))
            {
                string typeName = "BotonJXC.Forms.Controls." + BaseInfoControlResource.ResourceManager.GetString(tag);
                Type type = Type.GetType(typeName);
                if (type != null)
                {
                    value = assembly.CreateInstance(typeName) as BaseInfoControl;
                    _controls.Add(tag, value);
                    panel1.Controls.Add(value);
                    //value.Anchor = ((AnchorStyles)(((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left | AnchorStyles.Right)));
                    value.Dock = DockStyle.Fill;
                }
            }
            if (value != null)
            {
                value.Size = new System.Drawing.Size(panel1.Size.Width - 2, panel1.Size.Height - 2);
                value.Visible = true;
                _curControl = value;
                value.Tag = tag;
                this.Text = _nodeDics[tag];
                this.Refresh();
                this.Tag = tag;
                
                foreach (TreeNode n in ktvBaseInfo.Nodes)
                {
                    CheckedTreeNode(n, tag);
                }
            }
        }

        private void BaseInfoForm_Load(object sender, EventArgs e)
        {
            if (!this.ChangeModule)
            {
                panel1.Location = new System.Drawing.Point(3, 28);
                panel1.Size = new System.Drawing.Size(panel1.Width + ktvBaseInfo.Width+3, panel1.Height);
                kryptonGroupBox1.Visible = false;
            }
            if (!this.DesignMode)
            {
                LoadModuleTreeNode();
                ktvBaseInfo.ExpandAll();
                if (this.Tag != null)
                {
                    LoadBaseInfoControl(this.Tag.ToString());
                }

                ktvBaseInfo.AfterSelect += new TreeViewEventHandler(ktvBaseInfo_AfterSelect);
            }
        }
        private void LoadModuleTreeNode() {
            _nodeDics.Add("unit", "货品单位信息");
            _nodeDics.Add("productType", "货品类别信息");
            _nodeDics.Add("supplier", "供应商信息");
            _nodeDics.Add("store", "门店信息");
            _nodeDics.Add("department", "部门信息");
            _nodeDics.Add("warehouse", "仓库地信息");
            _nodeDics.Add("logisticsModel", "物流模式");
            _nodeDics.Add("assumeMode", "承但方式");
            _nodeDics.Add("region", "地区信息");
            _nodeDics.Add("role", "工作组信息");
            _nodeDics.Add("inPayItem", "收入/支出名目");
            _nodeDics.Add("invoiceType", "税票类型");
            _nodeDics.Add("processFormula", "加工/出品配方信息");
            _nodeDics.Add("payment", "付款方式");
            _nodeDics.Add("userInfo", "用户信息");
            _nodeDics.Add("userAuth", "用户权限");
            foreach (string k in _nodeDics.Keys)
            {
                TreeNode node = new TreeNode(_nodeDics[k]);
                node.Tag = k;
                ktvBaseInfo.Nodes[0].Nodes.Add(node);
            }
        }
        private void CheckedTreeNode(TreeNode node, string tag)
        {
            foreach (TreeNode n in node.Nodes)
            {
                if (tag.Equals(n.Tag))
                {
                    n.Checked = true;
                    ktvBaseInfo.SelectedNode = n;
                    this.Text = n.Text;
                    n.BackColor = System.Drawing.Color.Lavender;
                }
                else
                {
                    node.Checked = false;
                    CheckedTreeNode(n, tag);
                }
            }
        }

        private void tsbUpdate_Click(object sender, EventArgs e)
        {
            if (_curControl != null)
            {
                _curControl.SaveInfo();
            }
        }

        private void tsbAdd_Click(object sender, EventArgs e)
        {
            if (_curControl != null)
            {
                _curControl.AddInfo();
            }
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (_curControl != null)
            {
                if (MessageBox.Show("确定要删除选择的记录吗？", "删除确认", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    _curControl.DeleteInfo();
                }
            }
        }

        private void tsbSelect_Click(object sender, EventArgs e)
        {
            if (_enabledSelect && BaseInfoSelected != null)
            {
                if (_curControl.SelectedItems != null && _curControl.SelectedItems.Count > 0)
                    BaseInfoSelected(this, new BaseInfoSelectedEventArgs(_curControl.SelectedItems));
                else
                    MessageBox.Show("请选择!");
            }
        }

        private void BaseInfoForm_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void ktvBaseInfo_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string tag = e.Node.Tag as string;
            if (Utils.IsNotBlank(tag))
            {
                LoadBaseInfoControl(tag);
            }
        }

        private void ktvBaseInfo_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (ktvBaseInfo.SelectedNode != null)
            {
                ktvBaseInfo.SelectedNode.BackColor = ktvBaseInfo.Nodes[0].BackColor;
            }
        }
    }
}
